﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Windows;
    using System.Windows.Browser;

    public sealed class ApplicationContext
    {
        private string m_formDigest;
        private DateTime m_formDigestExpiration;
        private string m_requestToken;
        private SynchronizationContext m_uiThreadSynchronizationContext;
        private string m_url;
        private string m_viaUrl;
        private static ApplicationContext s_appContext;

        private ApplicationContext(string url, string viaUrl, string formDigest, int formDigestTimeoutSeconds, string requestToken, SynchronizationContext uiThreadSynchronizationContext)
        {
            this.m_url = url;
            this.m_viaUrl = viaUrl;
            this.m_formDigest = formDigest;
            this.m_formDigestExpiration = DateTime.UtcNow;
            this.m_formDigestExpiration = this.m_formDigestExpiration.AddSeconds(formDigestTimeoutSeconds * 0.75);
            this.m_requestToken = requestToken;
            this.m_uiThreadSynchronizationContext = uiThreadSynchronizationContext;
        }

        private static Dictionary<string, string> GetInitParamsFromUIThread()
        {
            Dictionary<string, string> dictionary = null;
            if (Application.Current.Host.InitParams != null)
            {
                dictionary = new Dictionary<string, string>();
                foreach (string str in Application.Current.Host.InitParams.Keys)
                {
                    dictionary[str] = Application.Current.Host.InitParams[str];
                }
            }
            return dictionary;
        }

        public string GetRequestUrl(string url)
        {
            string viaUrl = this.ViaUrl;
            if (!string.IsNullOrEmpty(viaUrl))
            {
                if (viaUrl.IndexOf("?") < 0)
                {
                    viaUrl = viaUrl + "?";
                }
                if (!viaUrl.EndsWith("?") && !viaUrl.EndsWith("&"))
                {
                    viaUrl = viaUrl + "&";
                }
                url = viaUrl + "MS.SP.url=" + HttpUtility.UrlEncode(url);
            }
            return url;
        }

        public static void Init(IDictionary<string, string> initParams)
        {
            InitPrivate(initParams, null);
        }

        public static void Init(IDictionary<string, string> initParams, SynchronizationContext uiThreadSynchronizationContext)
        {
            InitPrivate(initParams, uiThreadSynchronizationContext);
        }

        private static void InitPrivate(IDictionary<string, string> initParams, SynchronizationContext uiThreadSynchronizationContext)
        {
            string str4;
            if (initParams == null)
            {
                if (!Deployment.Current.Dispatcher.CheckAccess())
                {
                    ManualResetEvent resetEvent = new ManualResetEvent(false);
                    Deployment.Current.Dispatcher.BeginInvoke(delegate {
                        initParams = GetInitParamsFromUIThread();
                        resetEvent.Set();
                    });
                    if (!resetEvent.WaitOne(0x2710))
                    {
                        throw new InvalidOperationException();
                    }
                    resetEvent.Close();
                }
                else
                {
                    initParams = GetInitParamsFromUIThread();
                }
            }
            string str = null;
            if ((initParams != null) && initParams.TryGetValue("MS.SP.url", out str))
            {
                str = HttpUtility.UrlDecode(str);
            }
            string str2 = null;
            if ((initParams != null) && initParams.TryGetValue("MS.SP.viaUrl", out str2))
            {
                str2 = HttpUtility.UrlDecode(str2);
            }
            string str3 = null;
            if ((initParams != null) && initParams.TryGetValue("MS.SP.formDigest", out str3))
            {
                str3 = HttpUtility.UrlDecode(str3);
            }
            int result = 0;
            if ((initParams != null) && initParams.TryGetValue("MS.SP.formDigestTimeoutSeconds", out str4))
            {
                int.TryParse(str4, out result);
            }
            string str5 = null;
            if ((initParams != null) && initParams.TryGetValue("MS.SP.requestToken", out str5))
            {
                str5 = HttpUtility.UrlDecode(str5);
            }
            ApplicationContext context = new ApplicationContext(str, str2, str3, result, str5, uiThreadSynchronizationContext);
            s_appContext = context;
        }

        public static ApplicationContext Current
        {
            get
            {
                ApplicationContext context = s_appContext;
                if (context == null)
                {
                    Init(null, null);
                    context = s_appContext;
                }
                return context;
            }
        }

        public string FormDigest
        {
            get
            {
                return this.m_formDigest;
            }
        }

        public DateTime FormDigestExpiration
        {
            get
            {
                return this.m_formDigestExpiration;
            }
        }

        public string RequestToken
        {
            get
            {
                return this.m_requestToken;
            }
        }

        internal SynchronizationContext UIThreadSynchronizationContext
        {
            get
            {
                return this.m_uiThreadSynchronizationContext;
            }
        }

        public string Url
        {
            get
            {
                return this.m_url;
            }
        }

        public string ViaUrl
        {
            get
            {
                return this.m_viaUrl;
            }
        }
    }
}

